home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / prg_casm / jlvesa11.zip / JVEXMPLE.CPP < prev    next >
Text File  |  1996-01-03  |  11KB  |  433 lines

  1. /************************************************************************
  2. *  This file is part of JLVesa SVGA library v1.0
  3. *  Copyright 1995, 1996 Johannes Lehtinen
  4. *  All rights reserved
  5. ************************************************************************/
  6.  
  7. // All functions used should be ANSI C compatible
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "jlvesa.h"
  12.  
  13. // Define image size for demos
  14.  
  15. #define IMAGE_SIZE 100
  16.  
  17. // Function prototypes
  18.  
  19. void Demo_SinglePage(void);
  20. void Demo_MultiPage(void);
  21. void Error_Fatal(char *msg);
  22.  
  23. // Graphics figure used in demo
  24.  
  25. unsigned char jl_figure[]={
  26.    "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
  27.    "\000\000\000\017\017\017\017\017\017\000\000\000\000\000\000\000\000\000"
  28.    "\000\000\000\017\017\000\000\000\000\000\000\000\000\000\000\000\000\000"
  29.    "\000\017\017\000\000\000\000\000\000\000\000\000\000\000\000\000\000\017"
  30.    "\017\000\000\000\000\000\000\000\000\000\000\000\000\000\000\017\017\000"
  31.    "\000\000\000\000\000\000\000\000\000\000\000\000\000\017\017\000\000\017"
  32.    "\000\000\000\000\000\000\000\000\000\000\000\017\017\000\000\017\000\000"
  33.    "\000\000\000\000\000\000\000\000\000\017\017\000\000\017\000\000\000\000"
  34.    "\000\000\000\000\000\000\000\017\017\000\000\017\000\000\000\000\000\017"
  35.    "\017\000\000\000\000\017\017\000\000\017\000\000\000\000\000\017\017\000"
  36.    "\000\000\000\017\017\000\000\017\000\000\000\000\000\017\017\017\000\000"
  37.    "\017\017\017\000\000\017\017\017\017\000\000\000\017\017\017\017\017\017"
  38.    "\000\000\000\000\000\000\000\000\000\000\000\017\017\017\017\000\000\000"
  39.    "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
  40.    "\000\000\000\000"};
  41.  
  42. // Main function
  43.  
  44. void main(void)
  45. {
  46.    JVSVGAInfo vgainfo;
  47.  
  48.    // Get some information about the video card
  49.  
  50.    if(JVSVGA_GetInfo(&vgainfo)!=JV_SUCCESS)
  51.    {
  52.       // No VESA found
  53.  
  54.       printf("error: VESA interface not found!\n"
  55.              "Your video card is not VESA compatible. Try UNIVBE driver!\n");
  56.       exit(1);
  57.    }
  58.  
  59.    // Try to switch to 640x480/256 mode
  60.  
  61.    if(JVSVGA_SetMode(0x101)!=0)
  62.    {
  63.       // Failure
  64.  
  65.       printf("error: Can't set 640x480/256 mode\n"
  66.              "Your video card may not be VESA compatible or it may not\n"
  67.              "support the mode.\n");
  68.       exit(1);
  69.    }
  70.  
  71.    // Do single page demo
  72.  
  73.    Demo_SinglePage();
  74.  
  75.    // Do multiple page demo if possible
  76.  
  77.    if(vgainfo.memory>=16)
  78.       Demo_MultiPage();
  79.  
  80.    // Return to 80x25 mode
  81.  
  82.    JVBIOS_SetMode(3);
  83.  
  84.    // Give user some information about program
  85.  
  86.    printf("JVExample v1.0\n"
  87.           "Copyright 1995, 1996 Johannes Lehtinen\n"
  88.           "All rights reserved\n\n"
  89.           "This program is a simple example of how to use JLVesa SVGA library.\n"
  90.           "If graphics didn't seem to work OK, try loading UNIVBE driver before\n"
  91.           "executing this program.\n");
  92.  
  93.    exit(0);
  94. }
  95.  
  96. // Single page demo
  97.  
  98. void Demo_SinglePage(void)
  99. {
  100.    int i, n, a;
  101.    int color;
  102.    JVPalette palette;
  103.    JVPalette palette2;
  104.    void *image, *background;
  105.    int imgx, imgy;
  106.  
  107.    // Set black-red-purple-green-black palette
  108.  
  109.    for(color=0; color<256; color++)
  110.    {
  111.       if(color<64)         // Colors 0-63 (black - red)
  112.       {
  113.          palette[color][0]=color;
  114.          palette[color][1]=0;
  115.          palette[color][2]=0;
  116.       }
  117.       else if(color<128)   // Colors 64-127 (red-yellow)
  118.       {
  119.          palette[color][0]=63;
  120.          palette[color][1]=0;
  121.          palette[color][2]=color-64;
  122.       }
  123.       else if(color<192)   // Colors 128-191 (yellow-green)
  124.       {
  125.          palette[color][0]=63-(color-128);
  126.          palette[color][1]=color-128;
  127.          palette[color][2]=63-(color-128);
  128.       }
  129.       else                 // Colors 192-255 (green-black)
  130.       {
  131.          palette[color][0]=0;
  132.          palette[color][1]=63-(color-192);
  133.          palette[color][2]=0;
  134.       }
  135.    }
  136.    JVPalette_Set(palette);
  137.  
  138.    // Draw some simple shapes to screen
  139.  
  140.    color=0;
  141.    for(i=0; i<320; i++)
  142.    {
  143.       JVLine_Draw(319, 0, i, 479, color);
  144.       JVLine_Draw(320, 0, 639-i, 479, color);
  145.       JVLine_Draw(0, 479, 319-i, 0, color);
  146.       JVLine_Draw(639, 479, 320+i, 0, color);
  147.       if(++color>255)
  148.          color=0;
  149.    }
  150.  
  151.    // Scroll palette round
  152.  
  153.    for(i=0; i<512; i++)
  154.    {
  155.       // Create new palette
  156.  
  157.       for(n=0; n<256; n++)
  158.       {
  159.          color=(i+n) % 256;
  160.          for(a=0; a<3; a++)
  161.             palette2[color][a]=palette[n][a];
  162.       }
  163.  
  164.       // Wait for vertical retrace and set palette
  165.  
  166.       JVSVGA_WaitForVRetrace();
  167.       JVPalette_Set(palette2);
  168.    }
  169.  
  170.    // Demonstrate horizontal scrolling
  171.  
  172.    for(i=0; i<640; i+=4)
  173.    {
  174.       JVSVGA_WaitForVRetrace();
  175.       JVScreen_SetVisual(i, 0);
  176.    }
  177.    for(i=639; i>0; i-=4)
  178.    {
  179.       JVSVGA_WaitForVRetrace();
  180.       JVScreen_SetVisual(i, 0);
  181.    }
  182.  
  183.    // Demonstrate vertical scrolling
  184.  
  185.    for(i=0; i<480; i+=4)
  186.    {
  187.       JVSVGA_WaitForVRetrace();
  188.       JVScreen_SetVisual(0, i);
  189.    }
  190.    for(i=479; i>0; i-=4)
  191.    {
  192.       JVSVGA_WaitForVRetrace();
  193.       JVScreen_SetVisual(0, i);
  194.    }
  195.    JVSVGA_WaitForVRetrace();
  196.    JVScreen_SetVisual(0, 0);
  197.  
  198.    // Demonstrate image read/draw operations
  199.  
  200.    // Allocate memory for image and its background
  201.  
  202.    if((image=malloc(IMAGE_SIZE*IMAGE_SIZE))==NULL ||
  203.       (background=malloc(IMAGE_SIZE*IMAGE_SIZE))==NULL)
  204.       Error_Fatal("Memory allocation error");
  205.  
  206.    // Read image from upper right corner
  207.  
  208.    JVImage_Read(540, 0, IMAGE_SIZE, IMAGE_SIZE, image);
  209.    JVImage_Read(540, 0, IMAGE_SIZE, IMAGE_SIZE, background);
  210.    imgx=540;
  211.    imgy=0;
  212.  
  213.    // Move image around screen like sprite
  214.  
  215.    for(i=540; i>=0; i-=8)
  216.    {
  217.       JVSVGA_WaitForVRetrace();
  218.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  219.       imgx=i;
  220.       JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  221.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
  222.       JVSVGA_WaitForVRetrace();
  223.    }
  224.    for(i=0; i<380; i+=8)
  225.    {
  226.       JVSVGA_WaitForVRetrace();
  227.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  228.       imgy=i;
  229.       JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  230.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
  231.       JVSVGA_WaitForVRetrace();
  232.    }
  233.    for(i=0; i<540; i+=8)
  234.    {
  235.       JVSVGA_WaitForVRetrace();
  236.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  237.       imgx=i;
  238.       JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  239.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
  240.       JVSVGA_WaitForVRetrace();
  241.    }
  242.    for(i=380; i>=0; i-=8)
  243.    {
  244.       JVSVGA_WaitForVRetrace();
  245.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  246.       imgy=i;
  247.       JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  248.       JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
  249.       JVSVGA_WaitForVRetrace();
  250.    }
  251.    JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
  252.    free(image);
  253.    free(background);
  254.  
  255.    // Fade out palette
  256.  
  257.    for(i=128; i>=0; i--)
  258.    {
  259.       // Create new palette
  260.  
  261.       for(color=0; color<256; color++)
  262.          for(n=0; n<3; n++)
  263.             palette2[color][n]=(palette[color][n]*i)/128;
  264.  
  265.       // Set palette
  266.  
  267.       JVSVGA_WaitForVRetrace();
  268.       JVPalette_Set(palette2);
  269.    }
  270. }
  271.  
  272. // Multiple video page demo
  273.  
  274. void Demo_MultiPage(void)
  275. {
  276.    int i, n, color, x, y;
  277.    int xf, yf;
  278.    int fgcolor, bgcolor;
  279.    JVUByte *tile, *image, *background;
  280.    JVPalette palette, palette2;
  281.    int pos[2]={0,0}, speed[2]={6, 5};
  282.  
  283.    // Try to set logical width to 1024
  284.  
  285.    if(JVScreen_SetLogicalWidth(1024)!=1024)
  286.       Error_Fatal("Can not set logical width to 1024");
  287.  
  288.    // Allocate memory for tiles and images
  289.  
  290.    if((tile=(char *)malloc(128*128))==NULL ||
  291.       (image=(char *)malloc(64*64))==NULL ||
  292.       (background=(char *)malloc(64*64))==NULL)
  293.       Error_Fatal("Memory allocation error");
  294.  
  295.    // Create bouncing image
  296.  
  297.    JVRectangle_Draw(0, 0, 64, 64, 0);
  298.    for(i=0; i<16; i++)
  299.    {
  300.       color=255-i;
  301.       JVLine_Draw(16-i, i, 47+i, i, color);
  302.       JVLine_Draw(16-i, 63-i, 47+i, 63-i, color);
  303.       JVLine_Draw(i, 16, i, 47, color);
  304.       JVLine_Draw(63-i, 16, 63-i, 47, color);
  305.    }
  306.    JVImage_Read(0, 0, 64, 64, image);
  307.  
  308.    // Create background
  309.  
  310.    for(y=0; y<8; y++)
  311.       for(x=0; x<8; x++)
  312.       {
  313.          for(i=0; i<8; i++)
  314.             for(n=0; n<8; n++)
  315.             {
  316.                // Choose colors for this figure
  317.  
  318.                fgcolor=y | (x<<3) | (((i+n)%4) << 6);
  319.                bgcolor=fgcolor ^ 0xff;
  320.  
  321.                // Put figure to tile
  322.  
  323.                for(yf=0; yf<16; yf++)
  324.                   for(xf=0; xf<16; xf++)
  325.                      if(jl_figure[yf*16+xf]!=0)
  326.                         tile[(i*16+yf)*128+n*16+xf]=fgcolor;
  327.                      else
  328.                         tile[(i*16+yf)*128+n*16+xf]=bgcolor;
  329.             }
  330.  
  331.          // Put tile to screen
  332.  
  333.          JVImage_DrawLimited(x*128, y*128, 128, 128, tile, 0, 0, 1024, 1024);
  334.       }
  335.  
  336.    // Calculate palette
  337.  
  338.    for(color=0; color<256; color++)
  339.    {
  340.       palette[color][0]=(color & 3) * (color>>6) * 7;
  341.       palette[color][1]=((color>>2)&3) * (color>>6) * 7;
  342.       palette[color][2]=((color>>4)&3) * (color>>6) * 7;
  343.    }
  344.  
  345.    // Fade in palette
  346.  
  347.    for(i=0; i<=128; i++)
  348.    {
  349.       // Create palette
  350.  
  351.       for(color=0; color<256; color++)
  352.          for(n=0; n<3; n++)
  353.             palette2[color][n]=(palette[color][n]*i)/128;
  354.  
  355.       // Set palette
  356.  
  357.       JVSVGA_WaitForVRetrace();
  358.       JVPalette_Set(palette2);
  359.    }
  360.  
  361.    // Bounce image some time
  362.  
  363.    JVImage_Read(pos[0], pos[1], 64, 64, background);
  364.    for(n=0; n<2000; n++)
  365.    {
  366.       // Wait for vertical retrace
  367.  
  368.       JVSVGA_WaitForVRetrace();
  369.  
  370.       // Wipe off old image
  371.  
  372.       JVImage_DrawLimited(pos[0], pos[1], 64, 64, background, 0, 0, 1024, 1024);
  373.  
  374.       // Calculate new position
  375.  
  376.       for(i=0; i<2; i++)
  377.       {
  378.          pos[i]+=speed[i];
  379.          if(pos[i]<0) {
  380.             pos[i]=0;
  381.             speed[i]=-speed[i]; }
  382.          if(pos[i]>1024-64) {
  383.             pos[i]=1024-64;
  384.             speed[i]=-speed[i]; }
  385.       }
  386.  
  387.       // Reposition screen as necessary
  388.  
  389.       x=pos[0]-320+32;
  390.       if(x<0) x=0;
  391.       if(x>1024-640) x=1024-640;
  392.       y=pos[1]-240+32;
  393.       if(y<0) y=0;
  394.       if(y>1024-480) y=1024-480;
  395.       JVScreen_SetVisual(x, y);
  396.  
  397.       // Read background and place new image
  398.  
  399.       JVScreen_SetActive((long)pos[1]*1024L+(long)pos[0]);
  400.       JVImage_Read(0, 0, 64, 64, background);
  401.       JVImage_DrawOnLimited(0, 0, 64, 64, image, 0, 0, 1024, 1024);
  402.       JVScreen_SetActive(0);
  403.    }
  404.  
  405.    // Fade out palette
  406.  
  407.    for(i=128; i>=0; i--)
  408.    {
  409.       // Create new palette
  410.  
  411.       for(color=0; color<256; color++)
  412.          for(n=0; n<3; n++)
  413.             palette2[color][n]=(palette[color][n]*i)/128;
  414.  
  415.       // Set palette
  416.  
  417.       JVSVGA_WaitForVRetrace();
  418.       JVPalette_Set(palette2);
  419.    }
  420.  
  421.    return;
  422. }
  423.  
  424. // Fatal error handler
  425.  
  426. void Error_Fatal(char *msg)
  427. {
  428.    JVBIOS_SetMode(3);
  429.    fprintf(stderr, "error: %s\n", msg);
  430.    exit(1);
  431. }
  432.  
  433.